#include <stdio.h>
#include <time.h>

// 获取星期字符串，0-6对应周日-周六
const char* get_weekday_str(int wday) {
    static const char* weekdays[] = {
        "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
    };
    if (wday < 0 || wday > 6) return "";
    return weekdays[wday];
}

// 输入天数N，显示距今天N天前后日期及星期
void datefromtoday_5() {
    int days;
    printf("请输入距离今天的天数（整数）: ");
    if (scanf("%d", &days) != 1) {
        printf("输入格式错误。\n");
        return;
    }

    // 获取当前日期（当天0点）
    time_t now = time(NULL);
    struct tm now_tm = *localtime(&now);
    now_tm.tm_hour = 0;
    now_tm.tm_min = 0;
    now_tm.tm_sec = 0;
    time_t today = mktime(&now_tm);

    // 计算前后日期时间戳
    time_t before = today - (time_t)days * 24 * 60 * 60;
    time_t after  = today + (time_t)days * 24 * 60 * 60;

    struct tm before_tm = *localtime(&before);
    struct tm after_tm = *localtime(&after);

    printf("距离今天（%d-%02d-%02d）【%d】天的日期为：\n",
           now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, days);

    printf("比今天早 %d 天的日期：%d年%d月%d日 %s\n",
           days,
           before_tm.tm_year + 1900,
           before_tm.tm_mon + 1,
           before_tm.tm_mday,
           get_weekday_str(before_tm.tm_wday));

    printf("比今天晚 %d 天的日期：%d年%d月%d日 %s\n",
           days,
           after_tm.tm_year + 1900,
           after_tm.tm_mon + 1,
           after_tm.tm_mday,
           get_weekday_str(after_tm.tm_wday));
}
